<template>
  <div class="likes-container">
    <!-- 点赞按钮 -->
    <button @click="toggleLike" :class="{ liked: isLiked }">
      <i class="icon-heart"></i>
      <span v-if="isLiked" class="like-indicator">你</span>
    </button>
    <!-- 点赞数量 -->
    <span class="likes-count">{{ likes }} 赞</span>
    <!-- 点赞用户列表 -->
    <div v-if="showLikes" class="likes-list">
      <div v-for="user in likedUsers" :key="user.id" class="like-user">
        <img :src="user.avatar" alt="用户头像" class="user-avatar">
        <span class="user-name">{{ user.name }}</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Likes",
  props: {
    // 初始点赞数量（从父组件传入）
    initialLikes: {
      type: Number,
      default: 0
    },
    // 点赞用户列表（从父组件传入）
    likedUsers: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      likes: this.initialLikes, // 当前点赞数量
      isLiked: false, // 当前用户是否已点赞
      showLikes: false // 是否显示点赞用户列表
    };
  },
  methods: {
    toggleLike() {
      if (this.isLiked) {
        // 如果已经点赞，取消点赞
        this.likes -= 1;
        this.isLiked = false;
      } else {
        // 如果未点赞，进行点赞
        this.likes += 1;
        this.isLiked = true;
      }
      // 切换点赞用户列表的显示
      this.showLikes = !this.showLikes;
    },
    updateLikedUsers() {
      // 这里可以添加逻辑来更新点赞用户列表
      // 例如，从服务器获取最新的点赞用户列表
    }
  }
};
</script>

<style scoped>
.likes-container {
  display: flex;
  align-items: center;
  justify-content: flex-start; /* 调整对齐方式 */
  margin-top: 10px;
  position: relative; /* 为显示用户列表做准备 */
}

.likes-container button {
  background: none;
  border: none;
  cursor: pointer;
  outline: none;
  padding: 0;
}

.likes-container button.liked {
  color: red; /* 点赞状态的颜色 */
}

.likes-container button .icon-heart {
  font-size: 24px;
}

.like-indicator {
  font-size: 14px;
  margin-left: 5px;
}

.likes-count {
  margin-left: 8px;
  font-size: 16px;
  color: #333;
}

.likes-list {
  position: absolute;
  top: 30px; /* 调整列表位置 */
  left: 0;
  width: 100%;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  flex-direction: column; /* 调整布局方向 */
  max-height: 150px;
  overflow-y: auto;
}

.like-user {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  margin-right: 5px;
}

.user-name {
  font-size: 14px;
}
</style>